#!/bin/bash
#set current directory to the full path where the batch file is located.

#===========================================================================
#						ENVIRONMENT VARIABLES
#===========================================================================
export current_dir=$PWD
export PATH=$PATH:.:./thirdparty/lib:./lib:./customer_lib:./thirdpary/commonLib
PARAMS[6]=$@
	
#===========================================================================
#						FUNCTIONS DEFINISIONS
#===========================================================================
setJavaEnvironment()
{
	_JAVACMD=java
	if [ -f thirdparty/jdk/bin/java ]; then
		_JAVACMD=thirdparty/jdk/bin/java
		echo $_JAVACMD
	elif [ -f ../jdk/bin/java ]; then
		_JAVACMD=../jdk/bin/java
		echo $_JAVACMD
	elif [ -f $JAVA_HOME/bin/java ]; then
		_JAVACMD=$JAVA_HOME/bin/java
		echo $_JAVACMD
	fi
}

launch(){
	echo "the _JAVACMD variable = "$_JAVACMD
	echo "the DEBUG variable = "$DEBUG
	echo "remove the remark on the DEBUG variable to debug the runner."
	DEBUG=""
	#DEBUG="-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n"
	$_JAVACMD $DEBUG -Xms256m -Xmx512m $JMX -Djsystem.main=$JSYSTEM_MAIN -DentityExpansionLimit=1280000 -classpath ./lib/jsystem-launcher.jar  $SPLASH jsystem.framework.launcher.Launcher2 ${PARAMS[1]} ${PARAMS[2]} ${PARAMS[3]} ${PARAMS[4]} ${PARAMS[5]} ${PARAMS[6]}
}


#===========================================================================
#		PROGRAM FLOW
#===========================================================================
cd $current_dir

#if the jsystem.properties file doesn't exist, first create it from the .jsystembase file, and then continue.
if [ ! -f jsystem.properties ]; then
	if [ -f .jsystembase ]; then
		cp .jsystembase jsystem.properties
	fi
fi

setJavaEnvironment

export JSYSTEM_USED_CLASSPATH=$currentdir/lib/jsystem-launcher.jar
if [ ! "$JSYSTEM_CUSTOMER_JARS" == "" ]; then
	JSYSTEM_USED_CLASSPATH=$JSYSTEM_USED_CLASSPATH:$JSYSTEM_CUSTOMER_JARS
fi

launch

if [ $? -eq "6" ]; then
	echo "Exiting with Error code 6, retrying"
	launch
fi